package com.leesche.zcsp.node.web.controller.web.operation;

import com.leesche.zcsp.node.communication.service.SmsService;
import com.leesche.zcsp.node.utils.web.PagingResult;
import com.leesche.zcsp.node.utils.web.SessionManager;
import com.leesche.zcsp.node.utils.web.WebUtils;
import com.leesche.zcsp.node.utils.web.messge.WebResult;
import com.leesche.zcsp.node.web.constants.Constants;
import com.leesche.zcsp.node.web.model.SysAppInfo;
import com.leesche.zcsp.node.web.model.SysUserBankCard;
import com.leesche.zcsp.node.web.model.SysUserBusinessInfo;
import com.leesche.zcsp.node.web.service.SysAppInfoService;
import com.leesche.zcsp.node.web.service.SysUserBankCardService;
import com.leesche.zcsp.node.web.service.SysUserBusinessInfoService;
import com.leesche.zcsp.node.web.service.impl.data.read.*;
import com.leesche.zcsp.node.web.service.privilege.PrivilegeService;
import com.leesche.zcsp.node.web.vo.BankCard;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.util.Date;
import java.util.Map;

/**
 * @author Jerry Chan
 * @version V1.0
 *          ************ * Jerry Chan: * 2016/12/16 16:34: * 创建该类并实现业务逻辑: *********
 * @Title: UploadController.java
 * @Description: TODO(客户进件控制器)
 * @date 2016/12/16 16:34
 */
@Controller
@RequestMapping("/web/giveinfo")
public class GiveInfoToBankController {

    private final static Logger logger = Logger.getLogger(GiveInfoToBankController.class);

    @Autowired
    private PrivilegeService privilegeService;
    @Autowired
    private SysUserBankCardService sysUserBankCardService;
    @Autowired
    private SysUserBusinessInfoService sysUserBusinessInfoService;
    @Autowired
    private SmsService smsService;


    private static final String SHOW_VIEW = "web/giveinfotobank/listView";
    private static final String ADDBUSINESSINFO_VIEW = "web/giveinfotobank/addBusinessInfoView";
    private static final String AUDIT_VIEW = "web/giveinfotobank/auditView";


    @RequestMapping(value = "/showview", method = RequestMethod.GET)
    public String listView() {
        return SHOW_VIEW;
    }

    @RequestMapping(value = "{userid}/showUserBusinsessInfo", method = RequestMethod.GET)
    public String showUserBusinsessInfo(final @PathVariable("userid") long userid, Model model) {

        QuerySysUserBusinessInfoCondition condition = new QuerySysUserBusinessInfoCondition();
        condition.setUserid(userid);
        condition.setQueryAll(true);
        SysUserBusinessInfoDto userBusinessInfoDto = sysUserBusinessInfoService.findByCondition(condition);
        if (userBusinessInfoDto == null) {
            userBusinessInfoDto = new SysUserBusinessInfoDto();
        }

        userBusinessInfoDto.setUserid(userid);

        model.addAttribute("businessinfo", userBusinessInfoDto);
        return ADDBUSINESSINFO_VIEW;
    }

    /**
     * @param userid         用户ID
     * @param merchantnumber 商户号
     * @param merchantkey    商户密钥
     * @param rate           费率
     * @Title: saveUserBusinsessInfo()
     * @Description: TODO(保存用户的商业信息)
     * @author Jerry Chan
     * @date 2016/11/27 14:43
     */
    @ResponseBody
    @RequestMapping(value = "/saveUserBusinsessInfo", method = RequestMethod.POST)
    public Map<String, Object> saveUserBusinsessInfo(final Long userid, final String merchantnumber, final String merchantkey, final float rate,final String shopnumber) {
        try {

            SysUserBusinessInfo info = new SysUserBusinessInfo();
            info.setUserid(userid);
            info.setMerchantnumber(merchantnumber);
            info.setMerchantkey(merchantkey);
            info.setRate(rate);
            info.setText1(shopnumber);
            info.setAdminid(privilegeService.getCurrentPrincipal().getUserid());
            info.setUpdatetime(new Date());

            QuerySysUserBusinessInfoCondition condition = new QuerySysUserBusinessInfoCondition();
            condition.setUserid(userid);
            condition.setQueryAll(true);
            SysUserBusinessInfoDto userBusinessInfoDto = sysUserBusinessInfoService.findByCondition(condition);
            if (userBusinessInfoDto != null) {
                info.setBusinessinfoid(userBusinessInfoDto.getBusinessinfoid());
                sysUserBusinessInfoService.updateByPrimaryKeySelective(info);
            } else {
                sysUserBusinessInfoService.insertSelective(info);
            }
            return WebResult.success().toMap();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

    /**
     * @return
     * @Title: auditView(@PathVariable("bankcardid") Long id, Model model)
     * @Description: TODO(展示客户进件资料)
     * @author Jerry Chan
     * @date 2016/12/27 11:24
     */
    @RequestMapping(value = "{bankcardid}/auditview", method = RequestMethod.GET)
    public String auditView(@PathVariable("bankcardid") Long id, Model model) {

        QuerySysUserBankCardCondition condition = new QuerySysUserBankCardCondition();
        condition.setCardid(id);
        SysUserBankCardDto crad = sysUserBankCardService.findByCondition(condition);
        BankCard bankCard = new BankCard();
        if (crad != null) {
            bankCard.setCardid(crad.getCardid());
            bankCard.setUsername(crad.getUsername());
            bankCard.setAccounttype(crad.getAccounttype());
            bankCard.setBankcardnumber(crad.getBankcardnumber());
            bankCard.setBankcountry(crad.getBankcountry());
            bankCard.setBankprovince(crad.getBankprovince());
            bankCard.setBankcity(crad.getBankcity());
            bankCard.setBankofdeposit(bankCard.getBankofdeposit());
            bankCard.setBankname(crad.getBankname());
            bankCard.setBussinseclearingbank(crad.getBussinseclearingbank());
            bankCard.setCardholder(crad.getCardholder());
            bankCard.setPayunionbank(crad.getPayunionbank());
            bankCard.setMobile(crad.getMobile());
            bankCard.setIdcard(crad.getIdcard());
            bankCard.setClearingcycle(crad.getClearingcycle());
            bankCard.setCapitalclearingtype(crad.getCapitalclearingtype());
            bankCard.setCardconfirmed(crad.getCardconfirmed());
            bankCard.setBankofdeposit(crad.getBankofdeposit());
            bankCard.setComment(crad.getComment());

            bankCard.setIdcrardimgbehind(crad.getIdcrardimgbehind());
            bankCard.setIdcrardimgpositive(crad.getIdcrardimgpositive());
            bankCard.setBusinesslicense(crad.getBusinesslicense());
        }


        model.addAttribute("bankcard", bankCard);
        return AUDIT_VIEW;
    }

    @ResponseBody
    @RequestMapping(value = "/list.json", method = RequestMethod.GET)
    public Map<String, Object> listJson(final QuerySysUserBankCardCondition condition) {
        PagingResult<SysUserBankCardDto> pagingResult = sysUserBankCardService.findAllByPage(condition);
        Map<String, Object> json = WebResult.buildOf(pagingResult).toMap();
        return json;
    }

    /**
     * @param bankcard 客户进件信息
     * @Title: uploadUserLogo
     * @Description: TODO(绑定客户信息)
     * @author Jerry Chen
     * @date 2016年10月22日 上午3:05:21
     */
    @ResponseBody
    @RequestMapping(value = "/editGiveInfo", method = RequestMethod.POST)
    public Map<String, Object> editGiveInfo(SysUserBankCard bankcard) {
        try {
            bankcard.setAdminid(privilegeService.getCurrentPrincipal().getUserid());
            bankcard.setUpdatetime(new Date());
            int result = sysUserBankCardService.updateByPrimaryKeySelective(bankcard);
            if (result > 0)
                return WebResult.success().info("审核完成").toMap();
            else
                return WebResult.failure("系统有误请联系管理员").toMap();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }

    /**
     * @Title: uploadUserLogo
     * @Description: TODO(绑定客户信息)
     * @author Jerry Chen
     * @date 2016年10月22日 上午3:05:21
     */
    @ResponseBody
    @RequestMapping(value = "/verifyGiveInfo", method = RequestMethod.POST)
    public Map<String, Object> verifyGiveInfo(SysUserBankCard bankcard) {
        try {
            bankcard.setAdminid(privilegeService.getCurrentPrincipal().getUserid());
            bankcard.setUpdatetime(new Date());
            int result = sysUserBankCardService.updateByPrimaryKeySelective(bankcard);
            if (result > 0) {
                String verifyResult = "通过";

                if(bankcard.getCardconfirmed() != 1)
                    verifyResult = "未通过";

                smsService.giveInfoVerifyResultSMS(bankcard.getMobile(),bankcard.getCardholder(),verifyResult);
                return WebResult.success().info("审核完成").toMap();
            }else
                return WebResult.failure("系统有误请联系管理员").toMap();
        } catch (Exception e) {
            logger.error(e.getMessage(),e);
            return WebResult.failure(Constants.SYSTEM_ERROR_PROMPT_MSG).toMap();
        }
    }


}
